#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <string>
#include <vector>
#include <string.h>
#include <iostream>

#define SIZE 1024
using std::string;
using std::vector;
using std::cout;
using std::endl;

enum
{
    SUCC,
    DIVZERO,
    MODZERO,
    INVALIDOP
};

class Task
{
public:
    Task(int newsock = 0, sockaddr_in* client = nullptr)
    :_newsock(newsock),_client(client)
    {}   

    void run()
    {  
        char buffer[SIZE] = {0};

        memset(buffer,0,sizeof(buffer));
        if(read(_newsock,buffer,SIZE) < 0)
        {
            log(Error,"read error, errno is %d, error msg is %s", errno, strerror(errno));
        }

        // cout << "[ip:" << _client->sin_addr.s_addr << "]" << "says: ";
        // cout << buffer << endl;

        string msg = "server echo: " + string(buffer);
        if(write(_newsock, msg.c_str(), msg.size()) < 0)
        {
            log(Error,"write error, errno is %d, error msg is %s", errno, strerror(errno));
        }        

        close(_newsock);    //由于提供的是单次服务，故服务要及时关闭文件描述符！
    }

    void translate()
    {
        char buffer[SIZE] = {0};

        memset(buffer,0,sizeof(buffer));
        if(read(_newsock,buffer,SIZE) <= 0)
        {
            exit(0);
        }

        //翻译并返回结果

    }

    //仿函数设计
    void operator()()
    {
        this->run();
    }

private:
    int _newsock;
    sockaddr_in* _client;
};